package com.fudan.es.search;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;

public class SearchResponseEntity {

	private Long totalHit;
	private List<SearchHit> records;
	private String scollId;
	
	public static SearchResponseEntity build(SearchResponse searchResponse) {
		SearchResponseEntity searchResponseEntity = new SearchResponseEntity();
		SearchHits searchHits = searchResponse.getHits();
		searchResponseEntity.setTotalHit(searchHits.getTotalHits().value);
		SearchHit[] hits = searchHits.getHits();
		searchResponseEntity.setRecords(Arrays.asList(hits));
		searchResponseEntity.setScollId(searchResponse.getScrollId());
		return searchResponseEntity;
	}
	
	public List<Map<String,Object>> getRecordsAsMap() {
		List<Map<String,Object>> list = new LinkedList<>();
		if(records != null) {
			for (SearchHit record : records) {
				list.add(record.getSourceAsMap());
			}
		}
		return list;
	}
	public Long getTotalHit() {
		return totalHit;
	}
	public void setTotalHit(Long totalHit) {
		this.totalHit = totalHit;
	}
	public List<SearchHit> getRecords() {
		return records;
	}
	public void setRecords(List<SearchHit> records) {
		this.records = records;
	}
	public String getScollId() {
		return scollId;
	}
	public void setScollId(String scollId) {
		this.scollId = scollId;
	}
	
	
}
